package cn.wolfcdoe.business.service.impl;

import java.util.List;

import cn.wolfcdoe.business.domain.BusAppointment;
import cn.wolfcdoe.business.mapper.BusAppointmentMapper;
import cn.wolfcdoe.common.constant.BusServiceItemConstants;
import cn.wolfcdoe.common.utils.DateUtils;
import cn.wolfcdoe.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcdoe.business.mapper.BusStatementMapper;
import cn.wolfcdoe.business.domain.BusStatement;
import cn.wolfcdoe.business.service.IBusStatementService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * 结算单Service业务层处理
 * 
 * @author zhuo
 * @date 2023-04-13
 */
@Service
public class BusStatementServiceImpl implements IBusStatementService
{
    @Autowired
    private BusStatementMapper busStatementMapper;

    @Resource
    private BusAppointmentMapper busAppointmentMapper;

    /**
     * 查询结算单
     * 
     * @param id 结算单主键
     * @return 结算单
     */
    @Override
    public BusStatement selectBusStatementById(Long id)
    {
        return busStatementMapper.selectBusStatementById(id);
    }

    /**
     * 查询结算单列表
     * 
     * @param busStatement 结算单
     * @return 结算单
     */
    @Override
    public List<BusStatement> selectBusStatementList(BusStatement busStatement)
    {
        busStatement.setIsDelete(BusServiceItemConstants.STATEMENT_STATUS_NOT_DEL);
        return busStatementMapper.selectBusStatementList(busStatement);
    }

    /**
     * 新增结算单
     * 
     * @param busStatement 结算单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBusStatement(BusStatement busStatement)
    {
        if (busStatement.getAppointmentId() != null) {      //通过预约生成结算单
            //获取预约单
            BusAppointment busAppointment = busAppointmentMapper.selectBusAppointmentById(busStatement.getAppointmentId());
            //把对应的数据设置到busStatement对象中
            busStatement.setCustomerName(busAppointment.getCustomerName());             //客户姓名
            busStatement.setCustomerPhone(busAppointment.getCustomerPhone());         //客户号码
            busStatement.setServiceType(busAppointment.getServiceType());               //服务类型
            busStatement.setCarSeries(busAppointment.getCarSeries());                   //汽车类型
            busStatement.setLicensePlate(busAppointment.getLicensePlate());             //车牌号码
            busStatement.setInfo(busAppointment.getInfo());                             //备注
            busStatement.setActualArrivalTime(busAppointment.getActualArrivalTime());   //到店时间
        }
        //不是通过预约生成的结算单
        busStatement.setIsDelete(BusServiceItemConstants.STATEMENT_STATUS_NOT_DEL);       //设置未删除
        busStatement.setStatus(BusServiceItemConstants.STATEMENT_STATUS_CONSUMING);       //设置成消费中
        busStatement.setCreateBy(SecurityUtils.getUsername());                            //设置创建者从登入的账号中拿名字
        busStatement.setCreateTime(DateUtils.getNowDate());                               //设置生成时间
        return busStatementMapper.insertBusStatement(busStatement);
    }

    /**
     * 修改结算单
     * 
     * @param busStatement 结算单
     * @return 结果
     */
    @Override
    public int updateBusStatement(BusStatement busStatement)
    {
        Long id = busStatement.getId();
        Assert.notNull(id,"非法常数");
        BusStatement newBusStatement = busStatementMapper.selectBusStatementById(id);
        Assert.notNull(newBusStatement,"非法常数");
        int status = newBusStatement.getStatus();
        Assert.state(status == BusServiceItemConstants.STATEMENT_STATUS_CONSUMING,"已支付不能修改");
        Integer isDelete = newBusStatement.getIsDelete();
        Assert.state(isDelete==BusServiceItemConstants.STATEMENT_DELETE_YES,"已删除的不能修改");
        return busStatementMapper.updateBusStatement(busStatement);
    }

    /**
     * 批量删除结算单
     * 
     * @param ids 需要删除的结算单主键
     * @return 结果
     */
    @Override
    public int deleteBusStatementByIds(Long[] ids)
    {
        return busStatementMapper.deleteBusStatementByIds(ids);
    }

    /**
     * 删除结算单信息
     * 
     * @param id 结算单主键
     * @return 结果
     */
    @Override
    public int deleteBusStatementById(Long id)
    {
        return busStatementMapper.deleteBusStatementById(id);
    }

    @Override
    public BusStatement getStatementByAppointmentId(Long appointmentId) {
        return busStatementMapper.getStatementByAppointmentId(appointmentId);
    }

    @Override
    public int SoftDeleteBusStatementById(Long id) {
        Assert.notNull(id,"非法常数");
        BusStatement newBusStatement = busStatementMapper.selectBusStatementById(id);
        Assert.notNull(newBusStatement,"非法常数");
        Integer isDelete = newBusStatement.getIsDelete();
        Assert.state(isDelete==BusServiceItemConstants.STATEMENT_DELETE_YES,"已删除的不能删除");
        newBusStatement.setIsDelete(BusServiceItemConstants.STATEMENT_DELETE_NO);
        return busStatementMapper.updateBusStatement(newBusStatement);
    }
}
